//
//  FMDBDataHandler.m
//  A段项目
//
//  Created by dllo on 16/3/7.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "FMDBDataHandler.h"
#import "FMDatabase.h"
#import "Person.h"

@interface FMDBDataHandler ()

@property (nonatomic, retain) FMDatabase *db;



@end

@implementation FMDBDataHandler

- (void)dealloc {
    
    
    
    [_db release];
    
    [super dealloc];
    
}

+(instancetype)shareDataBaseHandler {
    
    static FMDBDataHandler *dataBase = nil;
    
    if (dataBase == nil) {
        
        dataBase = [[FMDBDataHandler alloc] init];
        
        return dataBase;
    }
    
    return dataBase;
}


- (void)openDB {
    
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"FMDB.db"];
    
    NSLog(@"%@",file);
    
    self.db = [FMDatabase databaseWithPath:file];
    
    if ([self.db open]) {
        
        NSLog(@"数据库打开成功");
        
    }else {
        
        NSLog(@"数据库打开失败");
        
    }
    
    
}

- (void)createTable {
    
    [self.db executeUpdate:@"CREATE TABLE PersonList(Name text,Address text,Number integer,Picture blob,Postion text,ChildName text)"];

    
}

/**插入数据. */
- (void)insertInfo:(Person *)per {
    
    [self.db executeUpdate:@"INSERT INTO PersonList(Name, Address, Number, Picture, Postion, ChildName) VALUES (?,?,?,?,?,?)",per.name,per.address,[NSNumber numberWithInteger:per.number],per.data,per.postion,per.childName];
}

/**更新数据. */
- (void)updateInfo:(Person *)per :(NSString *)address {
    
    
    [self.db executeUpdate:@"UPDATE PersonList SET Name = ?,Postion = ? ,ChildName = ? WHERE Address = ?",per.name, per.postion,per.childName, per.address];

}

/**查询数据. */


- (NSArray *)selectedInfo:(NSUInteger)number {
    
    FMResultSet *rs = [self.db executeQuery:@"SELECT * FROM PersonList WHERE Number = ?",[NSString stringWithFormat:@"%ld",number]];
    
    NSMutableArray *array = [NSMutableArray array];
    
    while ([rs next]) {
        
        Person *per = [[Person alloc] init];
        
      per.name = [rs stringForColumn:@"Name"];
        
        per.number = [rs intForColumn:@"Number"];
        
        per.address = [rs stringForColumn:@"Address"];
        
        per.data = [rs dataForColumn:@"Picture"];
        
        per.postion = [rs stringForColumn:@"Postion"];
        
        per.childName = [rs stringForColumn:@"ChildName"];
        
        [array addObject:per];
        
        [per release];
        
    }
    
    
    [rs close];
    
    return array;
}

- (NSArray *)selectedForNanme:(NSString *)name {
    
    
    FMResultSet *rs = [self.db executeQuery:@"SELECT * FROM PersonList WHERE Name = ?",name];
    
    NSMutableArray *array = [NSMutableArray array];
    
    while ([rs next]) {
        
        Person *per = [[Person alloc] init];
        
        per.name = [rs stringForColumn:@"Name"];
        
        per.number = [rs intForColumn:@"Number"];
        
        per.address = [rs stringForColumn:@"Address"];
        
        per.data = [rs dataForColumn:@"Picture"];
        
        per.postion = [rs stringForColumn:@"Postion"];
        
        per.childName = [rs stringForColumn:@"ChildName"];
        
        [array addObject:per];
        
        [per release];
        
    }
    
    
    [rs close];
    
    return array;
    
}

- (NSArray *)selectedForImage:(NSData *)data {
    
    FMResultSet *rs = [self.db executeQuery:@"SELECT * FROM PersonList WHERE Picture = ?",data];
    
    NSMutableArray *array = [NSMutableArray array];
    
    while ([rs next]) {
        
        Person *per = [[Person alloc] init];
        
        per.name = [rs stringForColumn:@"Name"];
        
        per.number = [rs intForColumn:@"Number"];
        
        per.address = [rs stringForColumn:@"Address"];
        
        per.data = [rs dataForColumn:@"Picture"];
        
        per.postion = [rs stringForColumn:@"Postion"];
        
        per.childName = [rs stringForColumn:@"ChildName"];
        
        [array addObject:per];
        
        [per release];
        
        
    }
    
    [rs close];
    
    return array;
    
}

- (void)deleteImage:(NSString *)name {
    
    [self.db executeUpdate:@"DELETE FROM PersonList WHERE Name = ?",name ];
    
    
}






@end
